const express = require('express')
const router = express.Router()
const { login } = require('../../../../common/controller/user')
const {
	SuccessModel,
	ErrorModel
} = require('../../../../common/model/res-model')

router.post('/login', async (req, res, next) => {
	const { username, password } = req.body
	const userData = await login(username, password)

	if (userData.username) {
		req.session.username = userData.username
		req.session.realname = userData.realname

		return res.json(new SuccessModel())
	}
	return res.json(new ErrorModel('登录失败'))
})

router.get('/login-test', (req, res, next) => {
	if (req.session.username) {
		return res.json({ errno: 0, msg: '已登录' })
	}
	res.json({ errno: -1, msg: '未登录' })
})

module.exports = router
